// pages/message/message/message.js
const api = require('../../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    articles: [],
    page: 1,
    pageSize: 12,
    hasMoreData: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.getArticle();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  getArticle: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    });
    wx.request({
      url: api.HomeArticle,
      data: {
        id: 14,
        page: that.data.page,
        per_page: 12,
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: function (res) {
        var goodsListTmp = that.data.articles;
        if (res.data.error_code == 0) {
          if (that.data.page == 1) {
            goodsListTmp = [];
          }
          if (res.data.articles.length < that.data.pageSize) {
            that.setData({
              articles: goodsListTmp.concat(res.data.articles),
              hasMoreData: false,
            });
          } else {
            that.setData({
              articles: goodsListTmp.concat(res.data.articles),
              hasMoreData: true,
              page: that.data.page + 1,
            });
          }
        }
      }
    });
    setTimeout(function () {
      wx.hideLoading();
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 1
    });
    that.getArticle();
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.hasMoreData) {
      that.getArticle();
    } else {
      wx.showToast({
        title: '没有更多数据',
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})